package com.clkj.cldisabledmanclient.mvp.myaction;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.jaylv.JayLvBaseAdapter;
import com.clkj.cldisabledmanclient.adapter.jaylv.ViewHolder;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.MyActionBean;
import com.clkj.cldisabledmanclient.bean.MyRecoverOrgBean;
import com.clkj.cldisabledmanclient.constant.Config;
import com.clkj.cldisabledmanclient.constant.TipStr;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class MyActionActivity extends BaseActivity implements MyActionListContract.View, PullToRefreshBase.OnRefreshListener2 {

    private PullToRefreshListView lv;
    private RelativeLayout rlEmpty;
    private TextView tvTipEmpty;

    private ProgressDialog progressDialog;
    private List<MyActionBean> myActionBeanList = new ArrayList<>();
    private JayLvBaseAdapter<MyActionBean> mAdapter;


    /**
     * 刷新标识
     */
    boolean isRefresh = false;
    /**
     * 是否可以加载更多标识
     */
    boolean canLoad = false;
    private Integer mPageIndex = 1;

    private MyActionListContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_action);
        initTitle(null, null, "我的活动", true, null);
        initView();
        initData();
        assignView();

        getMyActionList();
    }

    @Override
    public void initData() {

        mPresenter = new MyActionListPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {
        mAdapter = new JayLvBaseAdapter<MyActionBean>(this, myActionBeanList, R.layout.item_my_action) {
            @Override
            public void convert(ViewHolder helper, int position, MyActionBean item) {

                helper.setText(R.id.tv_actionName, item.getInformationTitle());
                helper.setText(R.id.tv_actionAuthor, item.getInformationAuthor());
                helper.setText(R.id.tv_actionTime, item.getInformationdate());
            }
        };

        lv.setAdapter(mAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                MyActionBean myActionBean = myActionBeanList.get(i);
                Intent toDetail=new Intent(MyActionActivity.this,MyActionDetailActivity.class);
                toDetail.putExtra("myActionBean",myActionBean);
                startActivity(toDetail);
            }
        });
    }

    @Override
    public void initView() {

        lv = (PullToRefreshListView) findViewById(R.id.lv);
        rlEmpty = (RelativeLayout) findViewById(R.id.rl_empty);
        tvTipEmpty = (TextView) findViewById(R.id.tv_tip_empty);
    }

    @Override
    public void getMyActionList() {

        mPresenter.getMyActionList(mPageIndex.toString(), Config.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void getMyActionListSuccess(List<MyActionBean> myActionBeans) {

        lv.onRefreshComplete();

        if (isRefresh) {
            myActionBeanList.clear();
            isRefresh = false;
        }

        if (myActionBeans.size() > 0) {

            if (myActionBeans.size() >= Config.DEFAULT_PAGE_SIZE) {
                lv.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            myActionBeanList.addAll(myActionBeans);

        } else {
            canLoad = false;
            lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (myActionBeanList.size() == 0) {
                lv.setEmptyView(rlEmpty);
                tvTipEmpty.setText("未查询到机构活动数据");
            } else {
                ToastUtil.showShort(this, TipStr.TIP_NOT_MORE_DATAS);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getMyActionListError(String tip) {

        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(MyActionListContract.Presenter presenter) {

        this.mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            //设置点击进度对话框外的区域对话框不消失
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setMessage("正在加载...");
            progressDialog.show();
        }
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex = 1;
        getMyActionList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex++;
        getMyActionList();
    }
}
